package com.joelaws.blackberry;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;

public class BrowseScreen extends BaseScreen {
	public BrowseScreen(final String userID, String[] users, Bitmap[] images,
			final int start) {

		for (int i = 0; i < users.length; i++) {

			final String otherUserID = users[i];

			final Bitmap image = images[i];

			add(new BitmapField(image));

			ButtonField browse = new ButtonField("View User");

			browse.setChangeListener(new FieldChangeListener() {
				public void fieldChanged(Field field, int context) {

					new Thread() {
						public void run() {
							String text = getString("getUserInfo?user="
									+ otherUserID);
							String[] data = split(text, ';');

							pushScreen(new ViewScreen(userID, otherUserID,
									image, data));

						}
					}.start();
				}
			});

			add(browse);

			add(new LabelField(""));
		}

		ButtonField browse = new ButtonField("Next");
		browse.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {

				new Thread() {
					public void run() {
						int next = start + 5;
						String text = getString(
								"getUserList?userID=" + userID + ":start="
										+ next).trim();
						String[] users = split(text, ';');
						Bitmap[] images = new Bitmap[users.length];

						for (int i = 0; i < users.length; i++) {
							images[i] = getPicture("getPicture?user="
									+ users[i]);
						}

						pushScreen(new BrowseScreen(userID, users, images, next));
					}
				}.start();
			}
		});

		add(browse);
	}

}
